﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Glucometer.ModuleHandler
{
    class BrowsingModuleHandler : GlucometerModuleHandler
    {

        public BrowsingModuleHandler(IHarware hardware, DeviceMemory memory, DeviceSettings setting, DeviceStatus status) : 
            base(hardware, memory, setting, status) { }

        public override bool handleEventInMode(SoftwareEventsType set)
        {
            if (set == SoftwareEventsType.LONG_CLICK)
            {
                endDevice(EndingType.VOLUNTARY, OperationMode.BROWSING);
                return true;
            }
            else if (set == SoftwareEventsType.SHORT_CLICK)
            {
                //GET THE NEXT RECORD BACKWARD AND DISPLAY IT
                int recordNumber = 0;
                TestRecord record = _deviceMemory.getNextRecord(true, out recordNumber);
                _hardware.showCurrentRecord(recordNumber.ToString() + ". " + record.getDisplayString(_settings.TestUnit));
                return true;
            }
            else if (set == SoftwareEventsType.DOUBLE_CLICK)
            {
                //GET THE NEXT RECORD FORWARD AND DISPLAY IT
                int recordNumber = 0;
                TestRecord record = _deviceMemory.getNextRecord(false, out recordNumber);
                _hardware.showCurrentRecord(recordNumber.ToString() + ". " + record.getDisplayString(_settings.TestUnit));
                return true;
            }
            return false;
        }

        public override bool doModeValidation(SoftwareEventsType set)
        {
            //CHECK IF THERE IS ANY RECORD OR NOT
            //IF ANY RECORD THEN MODE VALIDATION PASS OTHERWISE FAIL
            if (_deviceMemory.getRecordMemoryCount() > 0)
            {
                return true;
            }
            else 
            {
                //JUST SET THE LAST ERROR
                //BLINKING AND GOING TO ERROR ENDING IS COMMON PROCESS SO IT IS HANDLED AUTOMATICALLY
                //UNNECESSARY TO HANDLE HERE
                setLastError(ErrorCode.MODE_VALIDATION_ERROR, "There is no record in the memory.");
            }

            return false;
        }

        public override void loadMode()
        {
            //SET THE RECORD POSITION TO LATEST RECORD
            _deviceMemory.initializeBrowsePositionToLatestRecord();

            //SHOW THE LATEST RECORD
            int recordNumber = 0;
            TestRecord record = _deviceMemory.browseLatestRecord(out recordNumber);
            _hardware.showCurrentRecord(recordNumber.ToString() + ". " + record.getDisplayString(_settings.TestUnit));
        }

        public override bool doStatusCheck(SoftwareEventsType set)
        {
            //CHECK IF THE USB IS STILL CONNECTED
            if(_deviceStatus.usbConnected)
            {
                setLastError(ErrorCode.METERSTATUSCHECK_ERROR, "USB still connected.");
                return false;
            }

            //CHECK IF THE TEST STRIP IS STILL INSERTED
            if(_deviceStatus.testStripInserted)
            {
                setLastError(ErrorCode.METERSTATUSCHECK_ERROR, "Test strip still inserted.");
                return false;
            }

            //STATUS CHECK COMPLETE AND PASS SO RETURN TRUE
            return true;
        }

    }
}